<template>
	<view class="bg-gray1" style="min-height: 100vh;">
		<cu-custom :bgColor="'bg-'+theme.backgroundColor" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{mInfo.title}}</block>
		</cu-custom>
		<view class="padding">
			<view v-if="mInfo.content" class="padding-bottom">
				<view class="flex-row-between">
					<view class="font-30 text-bold">文字素材</view>
					<view class="">
						<button @click="copyData()" size="mini">复制文本</button>
					</view>
				</view>
				<view class="bg-white padding margin-top">{{mInfo.content}}</view>
			</view>
			<view v-if="mInfo.photos">
				<view class="flex-row-between">
					<view class="font-30 text-bold">图片素材</view>
					<view class="">
						<button @click="savePic" size="mini">保存图片</button>
					</view>
				</view>
				<view class="bg-white padding margin-top">
					<image :src="mInfo.photos" mode=""></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	const util = require("utils/util.js");
	const app = getApp();
	import api from 'utils/api'
	export default {
		data() {
			return {
				CustomBar: this.CustomBar,
				theme: app.globalData.theme, //全局颜色变量
				mInfo: {}
			}
		},

		onLoad(options) {
			app.initPage().then(res => {
				this.materialGet(options.id);
			});
		},

		methods: {
			materialGet(id) {
				api.materialGet(id).then(res => {
					this.mInfo = res.data ? res.data : {}
				});
			},
			//复制内容
			copyData() {
				uni.setClipboardData({
					data: this.mInfo.content,
					success: () => {
						uni.showToast({
							title: '复制成功',
							icon: 'success',
						});
					}
				});
			},
			// 保存图片
			savePic() {
				uni.getSetting({
					success: (res) => {
						if (res.authSetting['scope.writePhotosAlbum']) {
							this.trueSave()
						} else if (!res.authSetting.hasOwnProperty("scope.writePhotosAlbum")) {
							uni.authorize({
								scope: 'scope.writePhotosAlbum',
								success() {
									this.trueSave()
								}
							})
						} else {
							uni.showModal({
								title: "小程序未授权使用相册",
								content: "请先在设置中打开“添加到相册”开关，然后再次保存图片",
								confirmColor: "#e5432e",
								confirmText: "去设置",
								success(res) {
									if (res.confirm) {
										uni.openSetting({})
									}
								}
							})
						}
					},
					fail(res) {
						console.log(res, 'uni.getSetting');
					}
				})
			},
			trueSave() {
				uni.getImageInfo({
					src: this.mInfo.photos,
					success: (image) => {
						uni.saveImageToPhotosAlbum({
							filePath: image.path,
							success: () => {
								uni.showToast({
									title: '保存成功',
									icon: 'success',
								});
							}
						});
					}
				});
			}

		}
	}
</script>

<style>
	.img-box {
		width: 100%;
		height: 240rpx;
	}

	.article-image {
		width: 100%;
		height: 200rpx;
		margin: 0 auto;
	}
</style>